/**
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 * Author(s): Bikov Rinat <becase@altlinux.org>
 */
 
#ifndef SCANTEST_H
#define SCANTEST_H
//
#include <QMainWindow>
#include <QPoint>
#include <QStringList>
#include "ui_ScanTest.h"
//
template <typename T, size_t N>
char (&ArraySizeHelper(T (&array)[N]))[N];
#define arraysize(array) (sizeof(ArraySizeHelper(array)))

class QLabel;
class QFileDialog;
class QSettings;
class QDir;
class QImage;
struct Point{
	int x, y;	
};

class ScanTest : public QMainWindow, public Ui::ScanTest
{
Q_OBJECT
public:
	/**
	 * Create instance of this class
	 */
	ScanTest( QWidget * parent = 0, Qt::WFlags f = 0 );
protected:
	void closeEvent(QCloseEvent *event);
private:
	/**
	 * Pointer to window with open directory dialog
	 */
	QFileDialog * open;
	/**
	 * List of files in specified directory 
	 */
	QStringList fileList;
	/**
	 * Pointer to current image 
	 */
	QImage * image;
	/**
	 * Label for image displaying 
	 */
	QLabel * imageLabel;
	/**
	 * Path to directory, contains BMP files 
	 */
	QString directory;
	/**
	 * Pointer to program settings object 
	 */
	QSettings * settings;
	/**
	 * Pointer to specified directory object 
	 */
	QDir * dir;
	/**
	 * Array of bits in image 
	 */
	uchar * bits;
	/**
	 * Count of bytes in row of bitmap image 
	 */
	unsigned int bytesCount;
	void assignActions();
	void readSettings();
	void writeSettings();
	void treatImage();	
	/**
	 * Is bit in coordinate (x,y)
	 * @param x x coordinate of specified point
	 * @param y y coordinate of specified point
	 * @return <code>true</code>, if in specified point setted bit as 1,
	 * <code>false</code>, if in specified point bit value is 0. 
	 */
	bool isBit(int x, int y){
		return bits[y*bytesCount + (x>>3)] & (1<<((7-(x&7))&7));
	}
	/**
	 * Is bit in coordinate (p.x, p.y)
	 * @param p point object with (p.x, p.y) coordinate
	 * @return <code>true</code>, if in specified point setted bit as 1,
	 * <code>false</code>, if in specified point bit value is 0. 
	 */
	bool isBit(const Point &p){
		return isBit(p.x, p.y);
	}
	/**
	 * Set value for bit
	 * @param x x coordinate of point to set value
	 * @param y y coordinate of point to set value
	 * @param b value to set in specified point, if <code>true</code>, sets
	 * bit 1, if <code>false</code> - sets bit to 0.
	 */
	void setBit(int x, int y, bool b){
		if (x < 0 || y < 0){
			fprintf(stderr, "lowerflow x = %d y = %d\n", x, y);
			return;
		}
		if (x >= image->width() || y >= image->height()){
			fprintf(stderr, "overflow x = %d, y = %d\n", x, y);
			return;
		}
		if (b){
			bits[y*bytesCount + (x>>3)] |= (1<<((7-(x&7))&7));
		}else{
			bits[y*bytesCount + (x>>3)] &= (255^(1<<((7-(x&7))&7)));
		}
	}
	/**
	 * Set value for bit
	 * @param p pointer to Point object to set value for
	 * @param b value to set in specified point, if <code>true</code>, sets
	 * bit 1, if <code>false</code> - sets bit to 0.
	 */
	void setBit(const Point &p, bool b){
		setBit(p.x, p.y, b);
	}
	/**
	 * Draw fill black circle in point p with radius r
	 * @param p pointer to Point object of circle center
	 * @param r radius of drawing circle  
	 */
	void setBall(const Point &p, int r){
		int x, y;
		int nx = p.x + r;
		int ny = p.y + r;
		for (x = p.x - r; x <= nx; x++){
			for (y = p.y - r; y <= ny; y++){
				setBit(x, y, true);
			}
		}
	}
	/**
	 * Draw line from point a to point b
	 * @param a pointer to first point of segment
	 * @param b pointer to last point of segment   
	 */
	void line(const Point &a, const Point &b);
	/**
	 * Breadth-first search
	 */
	void bfs();
	/**
	 * Build convex hull for image 
	 */
	void convexHull();
private slots:
	/**
	 * Do action to open of directory 
	 */
	void openDirectory();
	/**
	 * Treat next file in the directory (Ctrl+N) 
	 */
	void openNextFile();
	/**
	 * Do action on directory opened
	 */
	void directoryOpened(const QString & s);
	/**
	 * Change view value 
	 */
	void fitToWindow(bool f);
};
#endif




